//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Documents;
using OpenLS.Core.Serialization;

namespace OpenLS.Drawing
{
    public class TextParagraphProperties : IOfficeXmlSerializableAs
    {
        //private TextBulletTypeface _TextBulletTypeface;
        private TextFont _bulletFont;
        private TextAlignType? algn;
        private TextBulletSize bulletSize;
        private TextCharacterProperties defRPr;
        private int indent;
        private TextSpacing lnSpc;
        private int? lvl;
        private int? marL;
        private int? marR;
        private TextSpacing spcAft;
        private TextSpacing spcBef;
        private ObservableCollection<TextTabStop> tabLst = new ObservableCollection<TextTabStop>();
        private TextBullet textBullet;
        private bool _buClrTx;
        private ColorBase _buClr;
        private bool _buFontTx; 

        public ObservableCollection<TextTabStop> TabStops
        {
            get { return tabLst; }
        }

        public int? LeftMargin
        {
            get { return marL; }
            set { marL = value; }
        }

        public int? RightMargin
        {
            get { return marR; }
            set { marR = value; }
        }

        public TextAlignType? Alignment
        {
            get { return algn; }
            set { algn = value; }
        }
 

        public int Indent
        {
            get { return indent; }
            set { indent = value; }
        }

        public int? Level
        {
            get { return lvl; }
            set { lvl = value; }
        }


       /* public TextBulletTypeface TextBulletTypeface
        {
            get { return _TextBulletTypeface; }
            set { _TextBulletTypeface = value; }
        }*/


        public TextCharacterProperties DpfRPr //\\rename
        {
            get { return defRPr; }
            set { defRPr = value; }
        }

        public TextBullet TextBullet
        {
            get { return textBullet; }
            set { textBullet = value; }
        }

        public TextSpacing LineSpacing
        {
            get { return lnSpc; }
            set { lnSpc = value; }
        }

        public TextSpacing SpacingBefore
        {
            get { return spcBef; }
            set { spcBef = value; }
        }

        public TextSpacing SpacingAfter
        {
            get { return spcAft; }
            set { spcAft = value; }
        }

        public TextBulletSize BulletSize
        {
            get { return bulletSize; }
            set { bulletSize = value; }
        }

        #region IOfficeXmlSerializableAs Members

        void IOfficeXmlSerializableAs.ReadXmlAs(string name, ReadContext context)
        {
            using (var c = context.Read(name))
            {
                LeftMargin = c.GetOptionalInteger("marL");
                RightMargin = c.GetOptionalInteger("marR");
                Indent = c.GetOptionalInteger("indent", 0);
                Level = c.GetOptionalInteger("lvl");
                Alignment = c.GetOptionalEnum<TextAlignType>("algn");
                lnSpc = TextSpacing.ReadSpacing("lnSpc", c);
                SpacingBefore = TextSpacing.ReadSpacing("spcBef", c);
                SpacingAfter = TextSpacing.ReadSpacing("spcAft", c);
                if (c.ReaderLocalName == "buClrTx")
                {
                    using (c.Read("buClrTx"))
                    {
                        BulletColorFollowsText = true;
                    }
                }
                else
                {
                    BulletColor = ColorBase.ReadOptionalColorAs("buClr", c);
                }
//                _buClrTx = c.ReadOptionalElement<TextBulletColorFollowText>("buClrTx");
  //              _buClr = ColorBase.ReadOptionalColorAs("buClr", c);
                this.TextBulletSize = TextBulletSize.ReadTextBulletSize(c);
                //\\EG_TextBulletColor
                //\\EG_TextBulletSize
                if (c.ReaderLocalName == "buFontTx")
                {
                    using (var c2 = c.Read("buFontTx"))
                    {
                        this.BulletFontFollowsText = true;
                    }
                }
                else
                {
                    this.BulletFont = c.ReadOptionalElementAs<TextFont>("buFont");
                }
               // _TextFont = 
               // TextBulletTypeface = TextBulletTypeface.ReadBulletTypeface(c);
                //\\EG_TextBullet
                TextBullet = TextBullet.ReadBullet(c);
                if (c.ReaderLocalName == "tabLst")
                {
                    using (ReadContext c2 = context.Read("tabLst"))
                    {
                        TabStops.Clear();
                        while (c2.ReaderLocalName == "tab")
                        {
                            TabStops.Add(c2.ReadElement<TextTabStop>());
                        }
                    }
                }
                DpfRPr = c.ReadOptionalElementAs<TextCharacterProperties>("defRPr");
                c.WarnIf("ext");
            }
        }

        public TextBulletSize TextBulletSize { get; set; }

        public ColorBase BulletColor
        {
            get { return _buClr; }
            set { _buClr = value;
                if (value != null)
                    BulletColorFollowsText = false;
            }
        }

        public bool BulletColorFollowsText
        {
            get { return _buClrTx; }
            set { _buClrTx = value;
            if (value)
                this._buClr = null;}
        }

        public TextFont BulletFont
        {
            get { return _bulletFont; }
            set { _bulletFont = value;
            if (value != null)
                _buFontTx = false;}
        }

        public bool BulletFontFollowsText
        {
            get { return _buFontTx; }
            set { _buFontTx = value;
            if (value)
                this._bulletFont = null;}
        }

        void IOfficeXmlSerializableAs.WriteXmlAs(string name, WriteContext context)
        {
            using (WriteContext c = context.Write(name))
            {
                c.SetOptionalInteger("marL", LeftMargin);
                c.SetOptionalInteger("marR", RightMargin);
                c.SetOptionalInteger("indent", Indent, 0);
                c.SetOptionalInteger("lvl", Level);
                c.SetOptionalEnum<TextAlignType>("algn", Alignment);
                c.WriteOptionalElementAs(lnSpc, "lnSpc");
                c.WriteOptionalElementAs(SpacingBefore, "spcBef");
                c.WriteOptionalElementAs(SpacingAfter, "spcAft");
                if (BulletColorFollowsText)
                {
                    using (c.Write("buClrTx")){}
                }
                else if (BulletColor != null)
                {
                    BulletColor.WriteXmlAs(c, "buClr");
                    
                }
                c.WriteOptionalElement(this.TextBulletSize);
                //\\EG_TextBulletColor
                //\\EG_TextBulletSize
                if (this.BulletFontFollowsText)
                {
                    using (var c2 = c.Write("buFontTx"))
                    {}
                }
                else
                {
                    c.WriteOptionalElementAs(this.BulletFont, "buFont");
                    
                }
                //\\EG_TextBullet
                c.WriteOptionalElement(TextBullet);
                if (TabStops != null && TabStops.Count > 0)
                {
                    using (WriteContext c2 = c.Write("tabLst"))
                    {
                        foreach (TextTabStop stop in TabStops)
                            c2.WriteElement(stop);
                    }
                }
                c.WriteOptionalElementAs(DpfRPr, "defRPr");
                c.WarnIf("ext");
            }
        }

        #endregion

        internal static TextParagraphProperties ReadTextParagraphProperties(string name, ReadContext c)
        {
            if (c.ReaderLocalName == name)
            {
                return c.ReadElementAs<TextParagraphProperties>(name);
            }
            return null;
        }

        internal IEnumerable<Setter> GetAllSetters()
        {
            if (Alignment != null)
            {
                switch (Alignment.Value)
                {
                    case TextAlignType.Left:
                        yield return new Setter(Paragraph.TextAlignmentProperty, TextAlignment.Left);
                        break;
                    case TextAlignType.Right:
                        yield return new Setter(Paragraph.TextAlignmentProperty, TextAlignment.Right);
                        break;
                    case TextAlignType.Center:
                        yield return new Setter(Paragraph.TextAlignmentProperty, TextAlignment.Center);
                        break;
                    default:
                        yield return new Setter(Paragraph.TextAlignmentProperty, TextAlignment.Justify);
                        break;
                }
            }
        }
    }
}